package main

import (
	"fmt"
	"strconv"
	"time"
)

func main() {

	decimalToString()
	stringToDecimal()
	datetimeToString()
	timeToString()

}

func decimalToString() {
	// 数类型
	var x3 uint8 = 123 // the set of all unsigned  8-bit integers (0 to 255)
	fmt.Println(fmt.Sprintf("%d", x3))

	s := fmt.Sprintf("%f", 123.456) // s == "123.456000"
	fmt.Println(s)
}

func stringToDecimal() {
	f := "3.14159265"
	if s, err := strconv.ParseFloat(f, 32); err == nil {
		fmt.Println(s) // 3.1415927410125732
	}
	if s, err := strconv.ParseFloat(f, 64); err == nil {
		fmt.Println(s) // 3.14159265
	}
}

func datetimeToString() {
	now := time.Now()      // current local time
	sec := now.Unix()      // number of seconds since January 1, 1970 UTC
	nsec := now.UnixNano() // number of nanoseconds since January 1, 1970 UTC

	fmt.Println(now)  // time.Time
	fmt.Println(sec)  // int64
	fmt.Println(nsec) // int64
}

func timeToString() {
	// this function returns the present time
	current_time := time.Now()

	// individual elements of time can
	// also be called to print accordingly
	fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
		current_time.Year(), current_time.Month(), current_time.Day(),
		current_time.Hour(), current_time.Minute(), current_time.Second())
	// 2021-05-24T22:58:47-00:00

	// formatting time using
	// custom formats
	fmt.Println(current_time.Format("2006-01-02 15:04:05"))
	fmt.Println(current_time.Format("2006-January-02"))
	fmt.Println(current_time.Format("2006-01-02 3:4:5 pm"))
	// 2021-05-24 22:58:47
	// 2021-May-24
	// 2021-05-24 10:58:47 pm

	current_time = time.Date(2021, 12, 31, 14, 23, 56, 0, time.UTC)
	// using inbuilt format constants
	// shown in the table above
	fmt.Println("ANSIC: ", current_time.Format(time.ANSIC))
	fmt.Println("UnixDate: ", current_time.Format(time.UnixDate))
	fmt.Println("RFC1123: ", current_time.Format(time.RFC1123))
	fmt.Println("RFC3339Nano: ", current_time.Format(time.RFC3339Nano))
	fmt.Println("RubyDate: ", current_time.Format(time.RubyDate))

	// ANSIC:  Mon May 24 22:58:47 2021
	// UnixDate:  Mon May 24 22:58:47 CST 2021
	// RFC1123:  Mon, 24 May 2021 22:58:47 CST
	// RFC3339Nano:  2021-05-24T22:58:47.6014432+08:00
	// RubyDate:  Mon May 24 22:58:47 +0800 2021
}
